package com.charles.utils;

import java.util.Stack;

/**
 * @author charles
 * @date 2022/9/5 10:33
 */
public class ConvertUtils {
    private static final char[] chars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM".toCharArray();

    public static String hex10To62(Long number) {
        Long rest = number;
        Stack<Character> stack = new Stack<Character>();
        StringBuilder result = new StringBuilder(0);
        while (rest != 0) {
            stack.add(chars[new Long((rest - (rest / 62) * 62)).intValue()]);
            rest = rest / 62;
        }
        for (; !stack.isEmpty(); ) {
            result.append(stack.pop());
        }
        return result.toString();
    }

    /**
     * 62进制转10进制
     *
     * @param sixty_str
     * @return
     */
    public static String hex62To10(String sixty_str) {
        Long dst = 0L;
        for (int i = 0; i < sixty_str.length(); i++) {
            char c = sixty_str.charAt(i);
            for (int j = 0; j < chars.length; j++) {
                if (c == chars[j]) {
                    dst = (dst * 62) + j;
                    break;
                }
            }
        }
        return dst.toString();
    }
}
